package com.example.private_auto_install.ftpGIt;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import java.io.*;

/**
 * @Description: 处理bat
 * @Author: by
 * @Date: 2022-02-28
 **/
public class BatHandle {

    public static Logger publishLogProcess = Logger.getLogger("publishLogProcess");
    /**
     * cmd /c dir 是执行完dir命令后关闭命令窗口.
     * cmd /k dir 是执行完dir命令后不关闭命令窗口.
     * cmd /c start dir 会打开一个新窗口后执行dir指令, 原窗口会关闭.
     * cmd /k start dir 会打开一个新窗口后执行dir指令, 原窗口不会关闭.
     */


    public static void main(String[] args) {
//        callCmd("c://test1.bat");
//        String result = callCmdReturnMsg("c://test1.bat");


        String deletePath = MainIndex.projectPath + File.separator + "bin-release" + File.separator + "web";
////        // 因java删除，总有一些被占用的因素，所以这里采用cmd命令进行删除
//        callCmd("rd /s /Q " + deletePath, deletePath);
        //
//        UtilFile.deleteFile(new File(deletePath));

        boolean flag = callCmdReturnMsg(MainIndex.projectPath + File.separator + "start.bat", MainIndex.projectPath);
        System.out.println("发布egret------------" + flag);


    }

    /**
     * 发布白鹭项目
     *
     * @return
     */
    public static boolean publishEgret(String rootPath) {
        //TODO 记录下，文件可以删除，但是文件夹有时候删除不成功
        //发布之前需要将已经发布过的包进行删除  删除失败
        File binRelease = new File(rootPath + File.separator + "bin-release");

        boolean deleteFlag = UtilFile.deleteFile(binRelease);

        boolean flag = callCmdReturnMsg(rootPath + File.separator + "start.bat", rootPath);
        return flag;
//        if (deleteFlag) {
//            boolean flag = callCmdReturnMsg(rootPath + File.separator + "start.bat", rootPath);
//            System.out.println("发布egret------------" + flag);
//            return flag;
//        } else {
//            System.out.println("发布egret------------失败，旧文件没删除");
//            return false;
//        }
    }

    /**
     * 单独执行cmd，不需要回执命令
     * 参数1的位置为命令；
     * 参数2的位置为执行命令的具体路径；
     * Runtime.getRuntime().exec("参数1",null,new File("参数2"));
     *
     * @param locationCmd
     */
    public static void callCmd(String locationCmd, String path) {
        System.out.println("执行命令" + locationCmd);
        Process child = null;
        try {
            child = Runtime.getRuntime()
                    .exec(locationCmd, null, new File(path));
            try {
                child.waitFor();
                int i = child.exitValue();
                if (i == 0) {
                    System.out.println("执行完成.");
                } else {
                    System.out.println("执行失败.");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            child.destroy();
        }
    }

    /**
     * 单独执行cmd，不需要回执命令
     *
     * @param locationCmd
     */
    public static boolean callCmdReturnMsg(String locationCmd, String path) {
        Process child = null;
        boolean resultFlag = false;
        String resultstr = "";
        BufferedReader br = null;
        InputStream fis = null;
        try {
            child = Runtime.getRuntime()
                    .exec(locationCmd, null, new File(path));

            //取得命令结果的输出流
            fis = child.getInputStream();
            //用一个读输出流类去读
            //用缓冲器读行
            br = new BufferedReader(new InputStreamReader(fis, "GB2312"));
            String line = null;
            //直到读完为止
            int i = 0;
            while ((line = br.readLine()) != null) {
                publishLogProcess.info(line);
                if (line.contains("main.min.js => ./main.min")) {  //解析符合自己需要的內容，获取之后，直接返回。
                    resultFlag = true;
                }
                resultstr = line + "\n";
                i++;
            }
            try {
                child.waitFor();
                int index = child.exitValue();
                if (index == 0) {
                    publishLogProcess.info("bat脚本执行完成.");
                } else {
                    publishLogProcess.warn("bat脚本执行失败.");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            child.destroy();
        }

        return resultFlag;
    }
}
